import { request, config } from 'utils';

const { api, superAdmin } = config;
const { VerifyPwd, nodeSave, nodeQuery } = api;

// 用户信息验证接口
export async function verifyPwd(params = {}) {
  const { UserPwd, UserName, type } = params;
  const jsonDt = {
    user: {
      UserName,
      UserPwd,
    },
  };
  return request(VerifyPwd, { data: jsonDt })
    .then(res => {
      const { data } = res;
      if (data) {
        return {
          type,
          status: 'ok',
          currentAuthority: data.UserName.includes(superAdmin) ? 'admin' : 'user', // 哪怕是admin用户也要是superAdmin才有admin权限
          LoginUser: data,
        };
      } else {
        return {
          status: 'error',
          type,
          currentAuthority: 'guest',
        };
      }
    })
    .catch(() => {
      return {
        status: 'error',
        type,
        currentAuthority: 'guest',
      };
    });
}

// 将 用户信息存在 node端
export async function saveUserInfo(params = {}) {
  const res = await request(nodeSave, {
    data: {
      ...params,
    },
    // loading: false,
  });
  return res;
}

// 查询当前用户信息
export async function queryUserInfo() {
  const res = await request(nodeQuery);
  return res;
}

// // 获取用户列表
// export async function getUserSortList() {
//   return request({ type: 1, name: 'GetUserSortList', body: { ds: {} } });
// }
